package org.gw.building.ctrl;

import com.dk.dfs.api.DefaultFileTicketService;
import com.dk.dfs.core.model.DownloadInfo;
import com.dk.dfs.core.model.FileInfo;
import org.gw.building.model.CommonFile;
import org.gw.building.repositories.FileRepository;
import org.gw.ylc.base.context.RequestContextHolder;
import org.gw.ylc.base.util.PermissionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Controller
@RequestMapping("file")
public class FileCtrl {
	@Autowired
	private FileRepository fileRepository;
	@Autowired(required = false)
	private DefaultFileTicketService defaultFileTicketService;
	@Value("#{config['file.web']}")
	private String fileServer;

	@RequestMapping("getServer")
	@ResponseBody
	public Object getServer() {
		return fileServer;
	}

	@RequestMapping("queryFile")
	@ResponseBody
	public Page<CommonFile> queryFile(String name) {
		return fileRepository.query(PermissionUtil.toLikeString(name), RequestContextHolder.get().getPageRequest());
	}

	@RequestMapping("addFile")
	@ResponseBody
	public Object addFile(CommonFile commonFile) {
		commonFile.setScsj(new Date());
		commonFile.setXzcs(0);
		return fileRepository.save(commonFile);
	}

	@RequestMapping("updateFile")
	@ResponseBody
	public Object updateFile(CommonFile commonFile) {
		return fileRepository.save(commonFile);
	}

	@RequestMapping("download")
	@ResponseBody
	@Cacheable(value = "download")
	public Object download(String id) {
		CommonFile commonFile = fileRepository.findOne(id);
		commonFile.addXzcs();
		commonFile.setZjxzsj(new Date());
		String[] split = commonFile.getFjId().split(",");
		if (split == null || defaultFileTicketService == null) {
			return "";
		}
		DownloadInfo downloadInfo = new DownloadInfo();
		downloadInfo.setName(commonFile.getOriginName());
		downloadInfo.addFile(new FileInfo(split[0], Long.parseLong(split[1])));
		String key = defaultFileTicketService.setDownloadInfo(downloadInfo);
		fileRepository.save(commonFile);
		return getServer() + "/download?key=" + key;
	}

	@RequestMapping("downloadPop")
	// @Cacheable(value = "downloadPop")
	public Object downloadPop(String id) {
		CommonFile commonFile = fileRepository.findOne(id);
		commonFile.addXzcs();
		commonFile.setZjxzsj(new Date());
		String[] split = commonFile.getFjId().split("/");
		if (split == null || defaultFileTicketService == null) {
			return "redirect:/index.html";
		}
		DownloadInfo downloadInfo = new DownloadInfo();
		downloadInfo.setName(commonFile.getOriginName());
		downloadInfo.addFile(new FileInfo(split[0], Long.parseLong(split[1])));
		String key = defaultFileTicketService.setDownloadInfo(downloadInfo);
		fileRepository.save(commonFile);
		return "redirect:" + getServer() + "/download?key=" + key;
	}

	@RequestMapping("downloadPopByHash")
	// @Cacheable(value = "downloadPop")
	public Object downloadPopByHash(String hash, long size, String name) {
		if (hash == null || defaultFileTicketService == null || !PermissionUtil.isAdmin()) {
			return "redirect:/index.html";
		}
		DownloadInfo downloadInfo = new DownloadInfo();
		downloadInfo.setName(name);
		downloadInfo.addFile(new FileInfo(hash, size));
		String key = defaultFileTicketService.setDownloadInfo(downloadInfo);
		return "redirect:" + getServer() + "/download?key=" + key;
	}

	@RequestMapping("deleteFile")
	@ResponseBody
	public Object deleteFile(final String[] ids) {
		fileRepository.delete(new Iterable<CommonFile>() {
			@Override
			public Iterator<CommonFile> iterator() {
				List<CommonFile> files = new ArrayList<CommonFile>();
				for (String id : ids) {
					CommonFile file = new CommonFile();
					file.setId(id);
					files.add(file);
				}
				return files.iterator();
			}
		});
		return ids.length;
	}
}
